package com.example.as_sum.application;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.example.as_sum.entity.Student;
import com.example.as_sum.util.DataBaseUtil;
import com.example.as_sum.util.OkHttpUtil;

import java.util.List;

public class MyService extends Service {
    public MyService() {
    }
    Handler mHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    if(msg.arg1==1){
                        Log.e("====>","接受失败");
                        stopSelf();
                    }
                    break;
                case 1:
                    Log.e("====>","接受到请求");
                    stopSelf();
                    break;
                case 2:
                    break;
            }
            return false;
        }
    });
    public void getHtmlByBaiDu(){
        Log.e("====>","发送请求");
        String url = "https://www.baidu.com/";
        OkHttpUtil.get(mHandler, url, null, 1);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("====>","Service onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("====>","Service onStartCommand");
        List<Student> students=DataBaseUtil.getQueryAll(Student.class);
        if(students!=null && students.size()!=0){
            getHtmlByBaiDu();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("====>","Service onDestroy");
    }
}
